﻿package com.game.view.layer
{
    import base.BaseHero;
    
    import com.dusk.util.TextUtil;
    import com.game.data.constant.FrameRate;
    import com.game.data.vo.PlayerDataVO;
    import com.game.engine.MainGame;
    import com.game.manager.NotificationManager;
    import com.game.module.heroBuff.HeroBuff;
    import com.game.view.component.BeAttackTimeBar;
    import com.game.view.component.BossBloodBar;
    import com.game.view.component.ComboPanel;
    import com.game.view.component.ComponentBase;
    import com.game.view.component.GoGoSigh;
    import com.greensock.TweenMax;
    import com.hurlant.util.Base64;
    
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormatAlign;
    
    public class GameInfo extends LayerBase
    {
        
        public function GameInfo()
        {
            super();
        }
        
        public var buffSprite:HeroBuff;
        public var bossBloodArray:Array = [];
        public var beAttArray:Array = [];//怪物被打次数条
        public var roleInfo:RoleInfo;
        internal var comboPanel:ComboPanel;//连击面板
        private var preBatterNum:int = 0;
        private var batterSi:int;
        private var infoSprite:Sprite;
        
        override protected function onAdded():void
        {
            //if(gc.savedBuff) addChild(gc.savedBuff);
            //gc.savedBuff.y = 200;
        }
        
        override protected function onRemoved():void
        {
            //gc.savedBuff = null;
        }
        
        public function showBuff():void
        {
            if (!buffSprite)
            {
                buffSprite = new HeroBuff();
                buffSprite.x = 0;
                buffSprite.y = 80;
                addChild(buffSprite);
            }
        }
        
        public function destroy():void
        {
            if (infoSprite)
            {
                if (contains(infoSprite))
                {
                    removeChild(infoSprite);
                }
                infoSprite = null;
            }
            if (buffSprite)
            {
                if (contains(buffSprite))
                {
                    removeChild(buffSprite);
                }
                buffSprite = null;
            }
            clearBossBlood();
            removeFromParent();
        }
        
        public function render(ratio:Number):void
        {
            if (infoSprite)
            {
                infoSprite.x -= 2 / ratio;
                if (infoSprite.x <= -infoSprite.width)
                {
                    removeChild(infoSprite);
                    infoSprite = null;
                }
            }
        }
        
        public function renderAnimate():void
        {
            batterSi++;
            if (batterSi % FrameRate.ANIMATE_FRAME_RATE == 0)
            {
                batter();
            }
            roleInfo.renderAnimate();
            if (buffSprite)
            {
                buffSprite.step();
            }
        }
        
        public function clearBossBlood():void
        {
            var item:ComponentBase;
            for each (item in bossBloodArray)
            {
                (item as BossBloodBar).removeFromParent();
            }
            bossBloodArray.length = 0;
            for each (item in beAttArray)
            {
                (item as BeAttackTimeBar).removeFromParent();
            }
            beAttArray.length = 0;
        }
        
        public function addBossBlood(param1:String, param2:Number):void
        {
            var bar:BossBloodBar = getChildByName(param1) as BossBloodBar;
            if (bar)
            {
                bar.update(param2);
                return;
            }
            bar = new BossBloodBar(param1);
            bar.x = 465;
            bar.y = 50 + bossBloodArray.length * 50;
            addChild(bar);
            bar.update(param2);
            bossBloodArray.push(bar);
        }
        
        public function addBeAttTime(param1:String, param2:Number):void
        {
            var bar:BeAttackTimeBar = getChildByName(param1) as BeAttackTimeBar;
            if (bar == null)
            {
                bar = new BeAttackTimeBar();
                bar.name = param1;
                bar.x = 465;
                bar.y = 50 + beAttArray.length * 50;
                addChild(bar);
                beAttArray.push(bar);
                bar.clear();
            }
            else if (param2 == 0)
            {
                bar.update(0, 6);
            }
            else
            {
                bar.update(param2);
            }
        }
        
        public function init():void
        {
            if (!NotificationManager.hasShowWatermark)
            {
                infoSprite = new Sprite();
                var content:String = Base64.decode("5qyi6L+O5p2l5Yiw6YCg5qKm6KW/5ri45LmL5LiJ55WM57q15qiq77yB5q2k54mI5pys5Li65aSV55y85Zui6Zif5omT6YCg55qE6YCg5qKm6KW/5ri45ZCM5Lq65L2c5ZOB77yM6K+35Yu/55So5LqO5Lu75L2V5ZWG5Lia55So6YCU77yB");
                var txt:TextField = TextUtil.creatTextField(content, TextUtil.UNIVERSAL_FONT, 20, TextFormatAlign.CENTER, 0xFF0000, false, false);
                infoSprite.addChild(txt);
                infoSprite.x = 900;
                infoSprite.y = 20;
                addChild(infoSprite);
                NotificationManager.hasShowWatermark = true;
            }
            showBuff();
        }
        
        public function setRole(hero:BaseHero, idx:int):void
        {
            var r:RoleInfo = new RoleInfo(hero, idx);
            r.x = 0;
            addChild(r);
            roleInfo = r;
        }
        
        private function removeComboPanel():void
        {
            if (comboPanel != null && contains(comboPanel))
            {
                removeChild(comboPanel);
                TweenMax.killTweensOf(comboPanel);
                comboPanel = null;
            }
        }
        
        private function batter():void
        {
            MainGame.getIns().battleData.maxCombo = MainGame.getIns().battleData.maxCombo > MainGame.getIns().battleData.curCombo ? MainGame.getIns().battleData.maxCombo : MainGame.getIns().battleData.curCombo;
            if (preBatterNum == MainGame.getIns().battleData.curCombo)
            {
                MainGame.getIns().battleData.curCombo = 0;
                preBatterNum = 0;
            }
            else
            {
                preBatterNum = MainGame.getIns().battleData.curCombo;
            }
        }
        
        public function addForwardPanel():void
        {
//			var sigh:GoGoSigh = new GoGoSigh();
//			sigh.x = 860;
//			sigh.y = 5 * 60;
//			sigh.scaleX = 0.7;
//			sigh.scaleY = 0.7;
//			addChild(sigh);
        }
        
        public function batterPanel():void
        {
            if (MainGame.getIns().battleData.curCombo >= 2)
            {
                if (comboPanel == null)
                {
                    comboPanel = new ComboPanel();
                    comboPanel.x = 694.95;
                    comboPanel.y = 234.95;
                    addChild(comboPanel);
                }
                else
                {
                    TweenMax.killTweensOf(comboPanel);
                }
                comboPanel.alpha = 1;
                TweenMax.to(comboPanel, 3.5, {
                    "alpha": 0,
                    "onComplete": removeComboPanel
                });
                comboPanel.addBatterNum(MainGame.getIns().battleData.curCombo);
            }
        }
    }
}
